####    This program is free software: you can redistribute it and/or modify
####    it under the terms of the GNU General Public License as published by
####    the Free Software Foundation, either version 3 of the License, or
####    (at your option) any later version.
####
####    This program is distributed in the hope that it will be useful,
####    but WITHOUT ANY WARRANTY; without even the implied warranty of
####    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
####    GNU General Public License for more details.
####
####    You should have received a copy of the GNU General Public License
####    along with this program.  If not, see <http://www.gnu.org/licenses/>.

d = open('output0.txt', 'w')    ### For debuging. Using 'w' or 'a' creates a new file if it is missing

### Opening the "ASCII art" character source file for reading
e = open('ASCII_alphabet.txt', 'r')    ### Using 'r' for readin only
e.seek(0)

### How do I find out how many lines a single "ASCII art" character takes?
###     How do I count the lines?    

## Detecting "ASCII art" chars and asinging them to data struture (tuple of tuples)
#### TODO: Create "syntax checking" of the alphabet.txt file
#### TODO: Find how to detect EOF (now alphabet.txt needs two '\n' chars at EOF
str_temp = ''
chr_tup = ()
ds_tup = ()

if(e.read(1) != ''):
    e.seek(0)
    r = e.read(1)
    e.seek(0)
    while(r != ''):
        ##print('This is r:' + r) ##for debuging
        stp = e.tell()       #latest stop point
        ##print(stp)           ##for debuging
        if(r != '\n'):         ##checking if is not a line end
            rl = e.readline()
            str_temp = (rl[0:len(rl)-1]) #rl[0:len(rl)-1]
            chr_tup += (str_temp, )
            ##print(srt_tuple)
            d.write(str_temp + '\n')
            stp = e.tell()
            ##print(stp)     ##for debuging
        else:
            r = e.read(1)
            if(r == '\n'):
                ##print('End of ASCII art character')
                d.write('End of ASCII art character\n')
                #d.write('\n')
                ds_tup += (chr_tup,) #ds_tup.append(chr_tup)
                chr_tup = ()
        r = e.read(1) ##r is incremented, checking for EOF
        stp = e.tell() ## stp must be updated!!!
        e.seek(stp-1)    ## step back to the start of line
        ##print('EndOFCylcle')   ##for debuging
    #print('EOF\n') ##for debuging
    #d.write('\nEOF')   ##for debuging
    d.write('\n')
    d.write(str(ds_tup) + '\n\n')   ##Extra newline is '\n' needed because character detection is retarded

### Text conversion to "ASCII art" text v1 #################################
###
### Problems:
    ## 1) No need to split the string, just "walk" the string char by char
    ##      1.1)Converting regular characters into ints or other numbers
    ##          a)Create an array with "alphabet" (sorted list?)
    ##          b)Use linear search for each character.
    ##      1.2)Deal with spaces between words.
    ## 2) Matching original text characters to "ASCII art" characters
    ## 3) Generating new strings of "ASCII art" characters

    ## Note: No lowercase letters yet!!! TODO: Add support for lowercase letters.
    ##       For now turn lowercase letters into uppercase letters

test_str = input('Enter a word: ')
num_tup = ()
ref_uppercase_tup = ()
ref_lowercase_tup = ()

for i in range(65, 91):         #filling uppercase tupple
    ref_uppercase_tup += (chr(i),)
for i in range(97, 123):        #filling lowercase tupple
    ref_lowercase_tup += (chr(i),)

###### Converting a regular text string into tuple of ints #################
for c in test_str:  #"walking" the string
    #print(c)         #for debuging
    if c != ' ':
        for y in range(len(ref_uppercase_tup)):
            if c == ref_uppercase_tup[y]:
                num_tup += (y,)
                break
        for u in range(len(ref_lowercase_tup)):
            if c == ref_lowercase_tup[u]:
                num_tup += (u,)
                break
    else:
        num_tup += (-1,)

###### Generating ""ASCII art" character string ##############################
ascii_tup = ()
ascii_row_str = ''
for i in range(0, len(ds_tup[0])):
    #print(ds_tup[0][i])
    for u in range(0, len(num_tup)):
        if(num_tup[u] >= 0):
            #print(num_tup[u]) #for debuging
            ascii_row_str += (ds_tup[num_tup[u]][i] + ' ')
        else:
            ascii_row_str += '   '
    ascii_tup += (ascii_row_str,)
    ascii_row_str = ''
    print(ascii_tup[i])

e.close()
d.close()
